package com.prs.crm.action.crm;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.Constrains;
import com.prs.crm.domain.crm.CustomerAddress;
import com.prs.crm.domain.crm.CustomerAppoint;
import com.prs.crm.service.crm.CustomerAddressService;

public class CustomerAddressAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private PaginationSupport pagination;
	private CustomerAddress customerAddress;
	private CustomerAddressService customerAddressService;
	private Integer[] addressIds;
	
	@Action("listCustomerAddress")
	public String listCustomerAddress(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				CustomerAddress.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.customerAddressService.listByPage(
				criteria, pagination));
		return SUCCESS;
	}
	@Action("editCustomerAddress")
	public String editCustomerAddress(){
		if(addressIds!=null){
			this.customerAddress=this.customerAddressService.get(addressIds[0]);
		}
		return SUCCESS;
	}
	@Action(value = "savebtm", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerAddress"),
			@Result(name = "error", type = "chain", location = "listCustomerAddress")})
	public String savebtm(){
		if(addressIds!=null){
			this.customerAddress=this.customerAddressService.get(addressIds[0]);
		}
		customerAddress.setRemark("默认");
		this.customerAddressService.submit(customerAddress);
		return SUCCESS;
	}
	@Action(value = "savebtq", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerAddress"),
			@Result(name = "error", type = "chain", location = "listCustomerAddress")})
	public String savebtq(){
		List<CustomerAddress> list=this.customerAddressService.getalladdress();
		for(int i=0;i<list.size();i++){
			CustomerAddress address=list.get(i);
			if(address.getRemark()!=null){
				address.setRemark("");
				this.customerAddressService.submit(address);
			}
		}
		return SUCCESS;
	}
	@Action(value = "saveCustomerAddress", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerAddress"),
			@Result(name = "error", type = "chain", location = "editCustomerAddress")})
	public String saveCustomerAddress(){
		if(customerAddress.getId()==null){
			this.customerAddressService.save(customerAddress);
		}else{
			this.customerAddressService.submit(customerAddress);
		}
		return SUCCESS;
	}
	@Action(value = "removeCustomerAddress", results = {
			@Result(name = "success", type = "redirect", location = "listCustomerAddress"),
			@Result(name = "error", type = "chain", location = "listCustomerAddress")})
	public String removeCustomerAddress(){
		this.customerAddressService.batchRemove(addressIds);
		return SUCCESS;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public CustomerAddress getCustomerAddress() {
		return customerAddress;
	}
	public void setCustomerAddress(CustomerAddress customerAddress) {
		this.customerAddress = customerAddress;
	}
	public CustomerAddressService getCustomerAddressService() {
		return customerAddressService;
	}
	public void setCustomerAddressService(
			CustomerAddressService customerAddressService) {
		this.customerAddressService = customerAddressService;
	}
	public Integer[] getAddressIds() {
		return addressIds;
	}
	public void setAddressIds(Integer[] addressIds) {
		this.addressIds = addressIds;
	}
	
}
